// 日期工具函数
export const dateUtils = {
  // 获取今天的日期
  getToday(): Date {
    return new Date();
  },

  // 获取从今天开始到一个月后的日期列表
  getDateRangeFromToday(days: number = 30): string[] {
    const today = new Date();
    const dates: string[] = [];

    for (let i = 0; i < days; i++) {
      const currentDate = new Date(today);
      currentDate.setDate(today.getDate() + i);

      // 格式化为 "MM/DD" 格式
      const month = String(currentDate.getMonth() + 1).padStart(2, '0');
      const day = String(currentDate.getDate()).padStart(2, '0');
      dates.push(`${month}/${day}`);
    }

    return dates;
  },

  // 获取从今天开始到一个月后的完整日期信息
  getDateRangeWithInfo(days: number = 30): Array<{
    date: string; // MM/DD 格式
    fullDate: string; // YYYY-MM-DD 格式
    dayOfWeek: string; // 星期几
    isWeekend: boolean; // 是否周末
    isToday: boolean; // 是否今天
  }> {
    const today = new Date();
    const dates: Array<{
      date: string;
      fullDate: string;
      dayOfWeek: string;
      isWeekend: boolean;
      isToday: boolean;
    }> = [];

    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];

    for (let i = 0; i < days; i++) {
      const currentDate = new Date(today);
      currentDate.setDate(today.getDate() + i);

      const month = String(currentDate.getMonth() + 1).padStart(2, '0');
      const day = String(currentDate.getDate()).padStart(2, '0');
      const year = currentDate.getFullYear();
      const dayOfWeek = weekDays[currentDate.getDay()];
      const isWeekend =
        currentDate.getDay() === 0 || currentDate.getDay() === 6;
      const isToday = i === 0;

      dates.push({
        date: `${month}/${day}`,
        fullDate: `${year}-${month}-${day}`,
        dayOfWeek,
        isWeekend,
        isToday,
      });
    }

    return dates;
  },

  // 格式化日期显示
  formatDateForDisplay(dateInfo: {
    date: string;
    dayOfWeek: string;
    isWeekend: boolean;
    isToday: boolean;
  }): string {
    if (dateInfo.isToday) {
      return `今天\n${dateInfo.date}`;
    }
    return `${dateInfo.dayOfWeek}\n${dateInfo.date}`;
  },

  // 检查日期是否在范围内
  isDateInRange(targetDate: string, startDate: Date, endDate: Date): boolean {
    const [month, day] = targetDate.split('/').map(Number);
    const year = startDate.getFullYear();
    const target = new Date(year, month - 1, day);

    return target >= startDate && target <= endDate;
  },

  // 获取日期对应的索引
  getDateIndex(targetDate: string, dateList: string[]): number {
    return dateList.indexOf(targetDate);
  },

  // 将旧的月份格式转换为新的日期格式
  convertMonthToDate(month: string): string[] {
    // 将 "2025/1月" 转换为该月的所有日期
    const [year, monthStr] = month.split('/');
    const monthNum = monthStr.replace('月', '');
    const monthIndex = parseInt(monthNum) - 1;

    const firstDay = new Date(parseInt(year), monthIndex, 1);
    const lastDay = new Date(parseInt(year), monthIndex + 1, 0);

    const dates: string[] = [];
    for (let day = firstDay.getDate(); day <= lastDay.getDate(); day++) {
      const month = String(monthIndex + 1).padStart(2, '0');
      const dayStr = String(day).padStart(2, '0');
      dates.push(`${month}/${dayStr}`);
    }

    return dates;
  },

  // 获取相对日期描述
  getRelativeDateDescription(dateStr: string): string {
    const today = new Date();
    const [month, day] = dateStr.split('/').map(Number);
    const targetDate = new Date(today.getFullYear(), month - 1, day);

    const diffTime = targetDate.getTime() - today.getTime();
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

    if (diffDays === 0) return '今天';
    if (diffDays === 1) return '明天';
    if (diffDays === 2) return '后天';
    if (diffDays > 0) return `${diffDays}天后`;
    if (diffDays === -1) return '昨天';
    if (diffDays < 0) return `${Math.abs(diffDays)}天前`;

    return dateStr;
  },

  // 根据视图类型获取日期范围
  getDateRangeForView(viewType: 'day' | 'week' | 'month'): string[] {
    const today = new Date();
    const dates: string[] = [];

    let days: number;
    switch (viewType) {
      case 'day':
        days = 1;
        break;
      case 'week':
        days = 7;
        break;
      case 'month':
        days = 30;
        break;
      default:
        days = 30;
    }

    for (let i = 0; i < days; i++) {
      const currentDate = new Date(today);
      currentDate.setDate(today.getDate() + i);

      const month = String(currentDate.getMonth() + 1).padStart(2, '0');
      const day = String(currentDate.getDate()).padStart(2, '0');
      dates.push(`${month}/${day}`);
    }

    return dates;
  },

  // 根据视图类型获取详细日期信息
  getDateRangeWithInfoForView(viewType: 'day' | 'week' | 'month'): Array<{
    date: string;
    fullDate: string;
    dayOfWeek: string;
    isWeekend: boolean;
    isToday: boolean;
    weekNumber?: number;
  }> {
    const today = new Date();
    const dates: Array<{
      date: string;
      fullDate: string;
      dayOfWeek: string;
      isWeekend: boolean;
      isToday: boolean;
      weekNumber?: number;
    }> = [];

    let days: number;
    switch (viewType) {
      case 'day':
        days = 1;
        break;
      case 'week':
        days = 7;
        break;
      case 'month':
        days = 30;
        break;
      default:
        days = 30;
    }

    const weekDays = ['日', '一', '二', '三', '四', '五', '六'];

    for (let i = 0; i < days; i++) {
      const currentDate = new Date(today);
      currentDate.setDate(today.getDate() + i);

      const month = String(currentDate.getMonth() + 1).padStart(2, '0');
      const day = String(currentDate.getDate()).padStart(2, '0');
      const year = currentDate.getFullYear();
      const dayOfWeek = weekDays[currentDate.getDay()];
      const isWeekend =
        currentDate.getDay() === 0 || currentDate.getDay() === 6;
      const isToday = i === 0;

      // 计算周数（对于周视图和月视图）
      let weekNumber: number | undefined;
      if (viewType === 'week' || viewType === 'month') {
        const firstDayOfYear = new Date(year, 0, 1);
        const pastDaysOfYear =
          (currentDate.getTime() - firstDayOfYear.getTime()) / 86400000;
        weekNumber = Math.ceil(
          (pastDaysOfYear + firstDayOfYear.getDay() + 1) / 7
        );
      }

      dates.push({
        date: `${month}/${day}`,
        fullDate: `${year}-${month}-${day}`,
        dayOfWeek,
        isWeekend,
        isToday,
        weekNumber,
      });
    }

    return dates;
  },

  // 格式化视图标题
  formatViewTitle(
    viewType: 'day' | 'week' | 'month',
    startDate?: Date
  ): string {
    const today = startDate || new Date();
    const year = today.getFullYear();
    const month = today.getMonth() + 1;
    const day = today.getDate();

    switch (viewType) {
      case 'day':
        return `${year}年${month}月${day}日`;
      case 'week':
        const weekStart = new Date(today);
        weekStart.setDate(today.getDate() - today.getDay());
        const weekEnd = new Date(weekStart);
        weekEnd.setDate(weekStart.getDate() + 6);

        const startMonth = weekStart.getMonth() + 1;
        const startDay = weekStart.getDate();
        const endMonth = weekEnd.getMonth() + 1;
        const endDay = weekEnd.getDate();

        if (startMonth === endMonth) {
          return `${year}年${startMonth}月${startDay}日-${endDay}日`;
        } else {
          return `${year}年${startMonth}月${startDay}日-${endMonth}月${endDay}日`;
        }
      case 'month':
        return `${year}年${month}月`;
      default:
        return '';
    }
  },
};
